angular.module('moti.controllers.adminslist', ['moti.services.adminsService', 'moti.directive.modal'])
    .controller('adminsListCtrl', ['$scope', 'AdminsInterface', '$modal', 'LoginName', function($scope, AdminsInterface, $modal, LoginName){

        //获取管理员列表
        AdminsInterface.getAdminsList().then(function(data){
            $scope.admins = data;
            //判断是否当前登录
            angular.forEach($scope.admins, function(value, key) {
                value.isLogin = value.name == LoginName;
            });
        });

        //删除管理员
        var dalete_modal = $modal({
            id : 'delete-modal',
            text : '确认要删除此管理员吗？该操作不可恢复！'
        });
        $scope.deleteAdmin = function(admin){
            dalete_modal.show();
            $scope.curAdminId = admin.admin_id;
        };
        $("#delete-btn").off("click").on("click", function(){
            AdminsInterface.deleteAdmin($scope.curAdminId).then(function(data){
                dalete_modal.hide();
                $modal({id : 'tip-modal', text : "删除管理员成功"}).show().autoHide();
                _.remove($scope.admins, function(n){
                    return $scope.curAdminId == n.admin_id;
                });
            });
        });

    }]);